<?php
namespace app\overlord\controller;
use app\AdminRest;

use app\ApiRest;
use app\overlord\model\Content;
use app\restaurant\model\Store;
use app\restaurant\model\StoreStaff;
use think\App;

use app\overlord\model\Active as model;

use app\overlord\model\Record;

use think\facade\Db;


class IndexAtv extends ApiRest
{

    protected $model;

    protected $record_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->record_model = new Record();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:35
     * @功能说明:列表
     */
    public function atvList(){

        $input = $this->_param;

        $dis[] = ['a.status','>',-1];

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['a.title','like','%'.$input['title'].'%'];
        }

        $dis[] = ['b.store_id','=',$this->getUserInfo()['store_id']];

        $data = $this->model->indexDataList($dis,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $count = $this->record_model->recordCount($v['id'],$this->getUserInfo()['store_id']);

                $v['excess_num'] = $v['num'] - $count;


                if($v['excess_num']<=0){

                    $v['atv_status'] = 4;

                    $v['signup_status'] = 4;
                }

            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-09 11:01
     * @功能说明:参加活动
     */
    public function recordAdd(){

        $input = $this->_input;
        //活动详情
        $atv   = $this->model->dataInfo(['id'=>$input['atv_id'],'status'=>1]);

        if(empty($atv)){

            $this->errorMsg('活动已下架');
        }

        if($atv['signup_start_time']>time()){

            $this->errorMsg('报名未开始');

        }

        if($atv['signup_end_time']<time()){

            $this->errorMsg('报名已结束');

        }
        //报名人数
        $have_count = $this->record_model->recordCount($input['atv_id'],$this->getUserInfo()['store_id']);

        if($atv['num']<=$have_count){

            $this->errorMsg('报名人数已满');

        }

        $where[] = ['user_id','=',$this->getUserId()];

        $where[] = ['store_id','=',$this->getUserInfo()['store_id']];

        $where[] = ['atv_id','=',$input['atv_id']];
        //是否报名
        $is_record = $this->record_model->recordInfo($where);

        if(!empty($is_record)){

            $this->errorMsg('你已经报过名了');

        }

        $insert = [

            'uniacid' => $this->_uniacid,

            'user_id' => $this->getUserId(),

            'atv_id'  => $input['atv_id'],

            'store_id'=> $this->getUserInfo()['store_id'],

            'cash'    => $atv['cash']
        ];

        $res = $this->record_model->dataAdd($insert);

        if($res!=1){

            $this->errorMsg('报名失败');

        }

        $id = $this->record_model->getLastInsID();

        return $this->success($id);

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:详情
     */
    public function atvInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);
        //门店信息
        $data['store'] = $this->model->selectStoreIndex($data['id']);
        //记录
        $dis = [

            'a.store_id' => $this->getUserInfo()['store_id'],

            'a.atv_id'   => $input['id']
        ];

        $data['record'] = $this->record_model->dataList($dis);

        $where[] = ['user_id','=',$this->getUserId()];

        $where[] = ['store_id','=',$this->getUserInfo()['store_id']];

//        $where[] = ['status','>',0];

        $where[] = ['atv_id','=',$input['id']];
        //是否报名
        $is_record = $this->record_model->recordInfo($where);

        $data['is_signup'] = !empty($is_record)?1:0;
        //报名状态
        $data['record_status'] = !empty($is_record)?$is_record['status']:0;
        //报名id
        $data['signup_id']     = !empty($is_record)?$is_record['id']:0;

        $content_model   = new Content();
        //活动内容
        $data['content'] = $content_model->atvContent($input['id']);

        $data['store_id'] = $this->getUserInfo()['store_id'];

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 17:34
     * @功能说明:
     */
    public function atvContent(){

        $input = $this->_param;

        $content_model   = new Content();
        //活动内容
        $data = $content_model->atvContent($input['atv_id']);

        return $this->success($data);
    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:提交资料
     */
    public function subInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id'],

        ];

        $record = $this->record_model->recordInfo($dis);

        if($record['status']==-1){
            //报名人数
            $have_count = $this->record_model->recordCount($record['atv_id'],$record['store_id']);

            if($record['atv_info']['num']<=$have_count){

                $this->errorMsg('报名人数已满');

            }
        }
        //活动详情
        $atv = $this->model->dataInfo(['id'=>$record['atv_id'],'status'=>1]);

        if(empty($atv)){

            $this->errorMsg('活动已下架');
        }

        if($atv['signup_end_time']<time()){

            $this->errorMsg('报名已结束');

        }

        Db::startTrans();

        $res = $this->record_model->dataUpdate($dis,['status'=>2]);

        if($res!=1){

            $this->errorMsg('提交失败');
        }

        $res = Db::name('longbing_card_overlord_content_record')->where(['record_id'=>$input['id']])->update(['status'=>0]);

        $time = time();

        foreach ($input['content'] as $value){

            $insert = [

                'uniacid'   => $this->_uniacid,

                'record_id' => $input['id'],

                'type'      => $value['type'],

                'content_id'=> $value['content_id'],

                'text'      => is_array($value['text'])?implode(',',$value['text']):$value['text'],

                'status'    => 1,

                'create_time' => $time

            ];

            $res = Db::name('longbing_card_overlord_content_record')->insert($insert);

        }

        Db::commit();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 11:08
     * @功能说明:记录详情
     */
    public function recordInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->record_model->recordInfo($dis);

        $staff_model = new StoreStaff();

        $data['staff_id'] = $staff_model->getStaffId($data['store_id']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 11:17
     * @功能说明:用户参与活动对列表
     */
    public function userRecordList(){

        $input = $this->_param;

        $dis[] = ['user_id','=',$this->getUserId()];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['status'])){

            if($input['status']==3){

                $dis[] = ['status','in',[3,-1]];


            }else{

                $dis[] = ['status','=',$input['status']];
            }

        }

        $data = $this->record_model->recordList($dis);

        $staff_model = new StoreStaff();

        if(!empty($data['data'])){

            foreach ($data['data'] as $k=>$v){

                $data['data'][$k]['staff_id'] = $staff_model->getStaffId($v['store_id']);

            }
        }

        return $this->success($data);

    }










}
